package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.entity.RankEntity;
import java.util.ArrayList;
import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;


/**
 * Created by dllo on 15/10/21.
 */
public class RankAdapter extends BaseAdapter {
    private Context mcontext;
    private List<RankEntity.Content> datas;

    public RankAdapter(Context context, List<RankEntity.Content> lists){
        datas = lists;
        mcontext = context;
    }

    public void addData(List<RankEntity.Content> lists){
        if (datas == null){
            datas = new ArrayList<RankEntity.Content>();
        } else {
            datas.clear();
        }
        datas.addAll(lists);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0){
            return datas.size();
        } else {
            return 0;
        }
    }

    @Override
    public RankEntity.Content getItem(int position) {
        if (datas != null && datas.size() > 0)
            return datas.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null ){
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.rank_listview_item,null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.rank_title_textView);
            holder.music_1 = (TextView) convertView.findViewById(R.id.rank_listView_item_textView1);
            holder.music_2 = (TextView) convertView.findViewById(R.id.rank_listView_item_textView2);
            holder.music_3 = (TextView) convertView.findViewById(R.id.rank_listView_item_textView3);
            holder.imageView = (ImageView) convertView.findViewById(R.id.rank_listView_item_imageView);
            holder.artist1 = (TextView) convertView.findViewById(R.id.rank_listView_item_artist_textView1);
            holder.artist2 = (TextView) convertView.findViewById(R.id.rank_listView_item_artist_textView2);
            holder.artist3 = (TextView) convertView.findViewById(R.id.rank_listView_item_artist_textView3);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        RankEntity.Content rankEntity = getItem(position);
        if (rankEntity != null){
            holder.title.setText(rankEntity.getName());
            holder.music_1.setText(rankEntity.getContent().get(0).getTitle());
            holder.music_2.setText(rankEntity.getContent().get(1).getTitle());
            holder.music_3.setText(rankEntity.getContent().get(2).getTitle());
            Picasso.with(mcontext).load(rankEntity.getPic_s192()).into(holder.imageView);
            holder.artist1.setText(rankEntity.getContent().get(0).getAuthor());
            holder.artist2.setText(rankEntity.getContent().get(1).getAuthor());
            holder.artist3.setText(rankEntity.getContent().get(2).getAuthor());
        }

        return convertView;
    }

    private class ViewHolder {
        private TextView title,music_1,music_2,music_3,artist1, artist2, artist3;
        private ImageView imageView;
    }


}
